﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Filter;
using Cofe.Core.Interfaces;
using Cofe.Core.Property;
using Cofe.Core.Service;

namespace Cofe.Data.Implements
{
    public class RepositoryPropertyProvider : DBPropertyProviderBase
    {
        #region Factory
        [Export(typeof(ICofeService))]
        [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEDB)]
        public class Factory : ISecondaryPropertyProviderFactory, INotifyImported
        {
            public IEnumerable<IPropertyProvider> GetSecoondaryPropertyProviders(IPropertyHost propertyHost)
            {
                if (_repositoryFactory != null &&
                    !(propertyHost.SecondaryPropertyProviders.Any((p) => p is RepositoryPropertyProvider)))
                    yield return new RepositoryPropertyProvider(propertyHost, _repositoryFactory);
            }

            public void OnImported(IServiceLocater serviceLocater)
            {
                _repositoryFactory = serviceLocater.TryFindService<IRepositoryFactory>();
            }

            private IRepositoryFactory _repositoryFactory = null;
            private IServiceLocater _serviceLocater = null;

            public IServiceLocater ServiceLocater
            {
                get { return _serviceLocater ?? CofeServices.ServiceLocater; }
                set { _serviceLocater = value; }
            }



        }
        #endregion

        #region Constructor

        public RepositoryPropertyProvider(IPropertyHost propertyHost, IRepositoryFactory repositoryFactory)
            : base(propertyHost, repositoryFactory,
            Cofe.Core.PropertyDefinitions.CacheDefinitions)
        {
            registerDBProperties(CacheProperties.LastCachedTimeUtc, (dbEntry) => dbEntry.LastCacheTime, () => CofeConstants.MinDateTime);
            if (propertyHost.SupportedDefinitions.Contains(Cofe.Core.PropertyDefinitions.CofeDirDefinitions, PropertyDefinitionComparer.CompareCategory))
                registerDBProperties(CacheProperties.LastListTimeUtc, (dbEntry) => dbEntry.LastListTime, () => CofeConstants.MinDateTime);
            registerDBProperties(CacheProperties.Id, (dbEntry) => dbEntry.Id, () =>
                {
                    string parseName = this.Entity.Behaviors.GetProperty<string>(CofeProperties.FullParseName);
                    if (parseName == "")
                        return CofeConstants.RootGuid;
                    return Guid.Empty;   
                });
        }

        #endregion

        #region Methods

        #endregion

        #region Public Properties

        /// <summary>
        /// For Debug only.
        /// </summary>
        public ISecondaryPropertyProviderFactory[] SecondaryPropertyProviderFactories { get; set; }

        /// <summary>
        /// For Debug only.
        /// </summary>
        public IPathParser[] PathParsers { get; set; }

        public IEntryConstructor EntryConstructor { get; internal set; }

        #endregion

    }
}
